package com.swu.pad.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;

import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;


public class PcsDictUtil {
	//随机16进制颜色
	public  String getRandColorCode(){  
		  String r,g,b;  
		  Random random = new Random();  
		  r = Integer.toHexString(random.nextInt(256)).toUpperCase();  
		  g = Integer.toHexString(random.nextInt(256)).toUpperCase();  
		  b = Integer.toHexString(random.nextInt(256)).toUpperCase();  
		    
		  r = r.length()==1 ? "0" + r : r ;  
		  g = g.length()==1 ? "0" + g : g ;  
		  b = b.length()==1 ? "0" + b : b ;  
		    
		  return r+g+b;  
		 }  

	
	public String readToString(String fileName) {  
        String encoding = "UTF-8";  
     
       
        File file = new File(fileName);  
        Long filelength = file.length();  
        byte[] filecontent = new byte[filelength.intValue()];  
        try {  
            FileInputStream in = new FileInputStream(file);  
            in.read(filecontent);
            in.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        try {  
            return new String(filecontent, encoding);  
        } catch (UnsupportedEncodingException e) {  
            System.err.println("The OS does not support " + encoding);  
            e.printStackTrace();  
            return null;  
        }  
    }
	
	
	
	public Map<String, Map<String,String>> wordMap() throws IOException {
    //1 funct 125 affect 131 cogmech  140 percept 250 relativ
	   
	    String wordString=this.readToString(this.getClass().getResource("/words.properties").getPath());
	    String indexString=this.readToString(this.getClass().getResource("/index.properties").getPath());
	    Map<String,String> coloreMap=new HashMap<String,String>();
	    Map<String,String> coloreMapWord=new HashMap<String,String>();
	    Map<String,Map<String, String>> result=new HashMap<String, Map<String,String>>();//返回值
	   
	    //1 提取类别   windows下 split换行 应该用 \r\n
	    String[] index=indexString.split("\r\n");
        //构造coloremap
	    for (int i = 0; i < index.length; i++) {
			String[] tempc=index[i].split(",");
			String[] tempDigit=tempc[0].split(" ");
			for (int j = 0; j < tempDigit.length; j++) {
				coloreMap.put(tempDigit[j],tempc[1]);
			}
		}
	    result.put("coloreMap",coloreMap);     
        //2处理 字典
	   String[] words=wordString.split("\r\n");
	   for (int i = 0; i < words.length; i++) {
		  String[] temp=words[i].split(" ");
		  if (coloreMap.get(temp[1])==null) {
			  coloreMapWord.put(temp[0],"#000000");//没有类别就填充为黑色
		  }else {
			  coloreMapWord.put(temp[0],coloreMap.get(temp[1]));
		   }

	   }
         
       result.put("coloreMapWord", coloreMapWord);      
       return result;
   
}
	
    
	public static  void main(String[] args) throws IOException {
		PcsDictUtil pcs=new PcsDictUtil();
		pcs.wordMap();
	}
	
	
	
	
}			